package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.ProductionMapper;
import com.village.zpark.pojo.Production;
import com.village.zpark.service.ProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class ProductionServiceImpl implements ProductionService {
    @Autowired
    private ProductionMapper productionMapper;
    @Override
    public List<Production> findAll() {
        return productionMapper.selectAll();
    }

    @Override
    public Production findById(Integer id) {
        return productionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addProduction(Production production) {
        return productionMapper.insert(production);
    }

    @Override
    public int updateProduction(Production production) {
        return productionMapper.updateByPrimaryKey(production);
    }

    @Override
    public int deleteProductionById(Integer id) {
        return productionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Production> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Production.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("resourcetype", "%" + param + "%")
                    .orLike("resourcelocation", "%" + param + "%")
                    .orLike("resourcearea", "%" + param + "%");  // 使用 orLike 来实现多字段模糊查询
        }
        List<Production> productions;
        if (param != null && !param.isEmpty()) {
            productions = productionMapper.selectByExample(example);
        } else {
            productions = productionMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(productions);
    }
}
